<?php
// +----------------------------------------------------------------------
// | OneKeyAdmin [ Believe that you can do better ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://onekeyadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: MUKE <513038996@qq.com>
// +----------------------------------------------------------------------
namespace plugins\lottery\admin\model;

use app\admin\model\User;
use think\Model;

class Lottery extends Model
{
    // 关联模型
    public function user()
    {
        return $this->hasOne(User::class, 'id', 'user_id');
    }
    
    // 搜索器
    public function searchKeywordAttr($query, $value)
    {
    	if (! empty($value)) {
	        $query->where("user_id|lottery_title|receiver_mobile|receiver_province|receiver_city|receiver_address|receiver_name","like", "%" . $value . "%");
	    }
    }
    
    public function searchDateAttr($query, $value, $array)
    {
        if (! empty($value)) { 
            $query->whereBetweenTime("create_time", $value[0], $value[1]);
        }
    }
    
    public function searchTypeAttr($query, $value)
    {
    	if (! empty($value)) {
	        $query->where("lottery_type", $value);
	    }
    }
    
    // 修改器、获取器
    public function getReceiverAttr($value, $array)
    {
        return "
        <div>{$array['receiver_mobile']}</div>
        <div>{$array['receiver_name']}</div>
        <div>{$array['receiver_province']}{$array['receiver_city']}{$array['receiver_district']}</div>
        <div>{$array['receiver_address']}</div>
        ";
    }
    
    public function getCreateTimeAttr($value, $array)
    {
        return date("Y-m-d H:i",$value);
    }
    
    public function getExchangeTimeAttr($value, $array)
    {
        return date("Y-m-d H:i",$value);
    }
}